﻿Public Class TIniFile
    Implements System.IDisposable

    Private _IniFileName As String

    Declare Function WritePrivateProfileString _
            Lib "KERNEL32.DLL" Alias "WritePrivateProfileStringA" ( _
                ByVal lpAppName As String, _
                ByVal lpKeyName As String, _
                ByVal lpString As String, _
                ByVal lpFileName As String) As Integer

    Declare Function GetPrivateProfileString _
            Lib "KERNEL32.DLL" Alias "GetPrivateProfileStringA" ( _
                ByVal lpAppName As String, _
                ByVal lpKeyName As String, _
                ByVal lpDefault As String, _
                ByVal lpReturnedString As String, _
                ByVal nSize As Integer, _
                ByVal lpFileName As String) As Integer

    Public Sub New(ByVal AIniFile As String)
        _IniFileName = AIniFile
    End Sub
    Public Function ReadBool(ByVal Section As String, ByVal Key As String, ByVal DefVal As Boolean) As Boolean
        Dim sTmp As String = Read(Section, Key, DefVal.ToString)
        If sTmp.ToUpper = "TRUE" Then
            Return True
        ElseIf sTmp.ToUpper = "FALSE" Then
            Return False
        Else
            Return DefVal
        End If
    End Function

    Public Function ReadColor(ByVal Section As String, ByVal Key As String, ByVal DefVal As Color) As Color
        Dim sTmp As String = Read(Section, Key, DefVal.ToArgb.ToString)
        Return Color.FromArgb(Common.StrToIntDef(sTmp, DefVal.ToArgb))
    End Function

    Public Function ReadString(ByVal Section As String, ByVal Key As String, ByVal DefVal As String) As String
        Return Read(Section, Key, DefVal)
    End Function

    Public Function ReadInteger(ByVal Section As String, ByVal Key As String, ByVal DefVal As Integer) As Integer
        Return Common.StrToIntDef(Read(Section, Key, DefVal.ToString), DefVal)
    End Function

    Private Function Read(ByVal ASection As String, ByVal AKey As String, ByVal ADefault As String) As String
        Dim strResult As String = Space(255)
        Call GetPrivateProfileString(ASection, AKey, ADefault, _
                                 strResult, Len(strResult), _
                                 _IniFileName)
        Return Microsoft.VisualBasic.Left(strResult, _
                                 InStr(strResult, Chr(0)) - 1)
    End Function

    Public Sub WriteColor(ByVal Section As String, ByVal Key As String, ByVal AValue As Color)
        Call Write(Section, Key, AValue.ToArgb.ToString)
    End Sub

    Public Sub WriteString(ByVal Section As String, ByVal Key As String, ByVal AValue As String)
        Call Write(Section, Key, AValue)
    End Sub

    Public Sub WriteInteger(ByVal Section As String, ByVal Key As String, ByVal AValue As Integer)
        Call Write(Section, Key, AValue.ToString)
    End Sub
    Public Sub WriteBool(ByVal Section As String, ByVal Key As String, ByVal AValue As Boolean)
        Call Write(Section, Key, AValue.ToString)
    End Sub

    Private Sub Write(ByVal ASection As String, ByVal AKey As String, ByVal AValue As String)
        Call WritePrivateProfileString(ASection, AKey, AValue, _IniFileName)
    End Sub

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If

            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
